package book;

public class SeqStackly implements Stackly {
	final int defaultSize=10;
	int top;//标记栈内元素个数，即栈顶元素
	Object[]stackly;
	 int maxStackSize;
	 public SeqStackly() {
		 initiate(defaultSize);
	 }
	 public SeqStackly(int sz) {
		 initiate(sz);
	 }
	private void initiate(int sz) {
		maxStackSize=sz;
		top=0;
		stackly =new Object[sz];
		
	}
	//实现把数据元素压入栈-----进栈
	public void push(Object obj) throws Exception {
		if(top==maxStackSize) {
			throw new Exception("堆栈已满！");
		}
		//进栈，站定标记加1
		stackly[top]=obj;
		top++;
	}

	//实现获取并删除栈顶数据元素-----退栈
	public Object pop() throws Exception {
		if(top==0) {
			throw new Exception("堆栈已空！");
		}
		//返回退栈数据元素，栈顶标记减1实现删除（实际并未删除）
		top--;
		return stackly[top];
	}

	//实现获取但不删除栈顶数据元素
	public Object getTop() throws Exception {
		if(top==0) {
			throw new Exception("堆栈已空！");
		}
		return stackly[top-1];
	}

	//实现判断栈是否为空
	public boolean notEmpty() {
		return (top>0);
	}

}
